package com.xiaoyung.easyis.app.demo.service;

import java.util.List;

import com.xiaoyung.easyis.app.demo.domain.Person;
import com.xiaoyung.easyisapisdk.common.form.DeleteByIdParam;
import com.xiaoyung.easyisapisdk.common.form.DeleteByIdsParam;
import com.xiaoyung.easyisapisdk.common.form.InsertParam;
import com.xiaoyung.easyisapisdk.common.form.SelectByIdParam;
import com.xiaoyung.easyisapisdk.common.form.SelectByIdsParam;
import com.xiaoyung.easyisapisdk.common.form.SelectParam;
import com.xiaoyung.easyisapisdk.common.form.UpdateParam;
import com.xiaoyung.easyisapisdk.common.util.PageData;


public class PersonService extends BaseService {
	private static final String TABLE_CODE = "st_ide_person_test";
	
	
	public Person add(Person person) {
		InsertParam data = new InsertParam();
		data.setTableCode(TABLE_CODE);
		PageData formData = new PageData(person);
		data.setFormData(formData);
		
		PageData result = dataService.insert(data);
		
		Person p = mapToBean(result, Person.class);
		
		return p;
		
		//String =>Date会报错
		/*Person p = new Person(); 
		try {
			BeanUtils.copyProperties(p, result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return p;*/
	}
	
	public Person edit(Person person) {
		UpdateParam param = new UpdateParam();
		param.setTableCode(TABLE_CODE);
		PageData formData = new PageData(person);
		param.setFormData(formData);
		
		PageData result = dataService.update(param);
		
		Person p = mapToBean(result, Person.class);
		
		return p;
	}
	
	
	public Integer deleteByIds(String[] ids) {
		DeleteByIdsParam param = new DeleteByIdsParam();
		param.setTableCode(TABLE_CODE);
		param.setIds(ids);
		
		Integer count = dataService.deleteByIds(param);
		
		return count;
	}
	
	public Integer deleteById(String id) {
		DeleteByIdParam param = new DeleteByIdParam();
		param.setTableCode(TABLE_CODE);
		param.setId(id);
		
		Integer count = dataService.deleteById(param);
		
		return count;
	}
	
	public List<Person> select(SelectParam param) {
		if(param==null) {
			param = new SelectParam();
		}
		param.setTableCode(TABLE_CODE);
		
		List<PageData> list = dataService.select(param);
		
		List<Person> beanList = mapToBean(list, Person.class);
		
		return beanList;
	}
	
	public Person selectById(String id) {
		SelectByIdParam param = new SelectByIdParam();
		param.setTableCode(TABLE_CODE);
		param.setId(id);
		
		
		PageData pd = dataService.selectById(param);
		
		Person bean = mapToBean(pd, Person.class);
		
		return bean;
	}
	
	public List<Person> selectByIds(String[] ids) {
		SelectByIdsParam param = new SelectByIdsParam();
		param.setTableCode(TABLE_CODE);
		param.setIds(ids);
		
		
		List<PageData> pdList = dataService.selectByIds(param);
		
		List<Person> list = mapToBean(pdList, Person.class);
		
		return list;
	}
	
	/*public List<Person> selectMultipleTable(SelectMultipleTableParam param) {
		if(param==null) {
			param = new SelectMultipleTableParam();
		}
		param.setMutipleTablQuery(null);
		
		List<PageData> pdList = dataService.selectMultipleTable(param);
		
		List<Person> list = mapToBean(pdList, Person.class);
		return list;
	}*/
	
	
	public PageData test(PageData formData) {
		InsertParam data = new InsertParam();
		data.setTableCode(TABLE_CODE);
		//PageData formData = new PageData();
		data.setFormData(formData);
		
		PageData result = api.dataService.insert(data);
		return result;
	}
	
	public PageData test2(Person formData) {
		InsertParam data = new InsertParam();
		data.setTableCode(TABLE_CODE);
		PageData formDataPd = new PageData(formData);
		data.setFormData(formDataPd);
		
		PageData result = api.dataService.insert(data);
		return result;
	}
	
	public static void main(String[] args) {
		PersonService personService = new PersonService();
		PageData pd = new PageData();
		pd.put("title", "xx");
		
		PageData result = personService.test(pd);
		
		System.out.println(result);
	}

	
}
